﻿

using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.SystemSet;
using SmartWMS.EntityFramework;
using SmartWMS.Server.Repository;
using SmartWMS.Server.SystemSets.StaffServices.Dto;

namespace SmartWMS.Server.SystemSets.StaffServices
{
    public class StaffService : IStaffService
    {
        private readonly IRepository<Staff> _staffRepository;
        private readonly IRepository<StaffRoles> _staffRolesRepository;
        private readonly SmartWMSContext _smartHospitalContext;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="staffRepository"></param>
        /// <param name="staffRolesRepository"></param>
        /// <param name="smartHospitalContext"></param>
        public StaffService(IRepository<Staff> staffRepository, IRepository<StaffRoles> staffRolesRepository, SmartWMSContext smartHospitalContext)
        {
            _staffRepository = staffRepository;
            _staffRolesRepository = staffRolesRepository;
            _smartHospitalContext = smartHospitalContext;
        }

        /// 人员分配角色
        /// </summary>
        /// <param name="staffId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> AddStaffRoleAsync(StaffRoleInput input)
        {
            var staffId = input.StaffID;
            var roleIds = input.RoleID;
            var i = 0;
            //删除原有的角色
            var staffRoles = _staffRolesRepository.GetAllListAsync().Result;
            staffRoles = staffRoles.Where(x => x.StaffID == staffId).ToList();

            if (staffRoles.Any())
            {
                i = await _staffRolesRepository.DeleteAsync(staffRoles);
            }

            if (!string.IsNullOrEmpty(roleIds))
            {
                var staffRoleList = new List<StaffRoles>();
                var roleIdsList = roleIds.Split(',');

                foreach (var roleId in roleIdsList)
                {
                    staffRoleList.Add(new StaffRoles()
                    {

                        StaffID = staffId,
                        RoleID = Convert.ToInt32(roleId)
                    });
                }
                i = await _staffRolesRepository.InsertAsync(staffRoleList);
            }
            return i > 0 ? Response<bool>.Ok(true) : Response<bool>.Ok(false);

        }

        /// <summary>
        /// 获取人员角色关联表数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<int> GetRoleByIdAsync(int id)
        {
            var list = _smartHospitalContext.StaffRoles.Where(x => x.StaffID == id).ToList();
            List<int> ids = new List<int>();
            foreach (var item in list)
            {
                ids.Add(item.RoleID);
            }
            return ids;
        }
        /// <summary>
        /// 人员列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<List<StaffInput>>> GetStaffListAsync()
        {
            var staffs = await _staffRepository.GetAllListAsync();
            var result = staffs.MapToList<StaffInput>();
            return Response<List<StaffInput>>.Ok(result);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<StaffInput>> LoginAsync(StaffInput input)
        {
            input.StaffPwd = MD5Helper.Get16MD5Two(input.StaffPwd);
            var list = _smartHospitalContext.Staff.FirstOrDefault(x => x.StaffName == input.StaffName && x.StaffPwd == input.StaffPwd);
            var listmap = list.MapTo<StaffInput>();
            if (list == null)
            {
                return Response<StaffInput>.Fail("账号或密码错误，请联系管理员");
            }
            else
            {
                return Response<StaffInput>.Ok(listmap);
            }
        }
    }
}
